{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-22T09:00:52.187943Z",
     "start_time": "2025-01-22T09:00:52.104053Z"
    }
   },
   "source": [
    "# 导包\n",
    "import requests"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:17:33.048053Z",
     "start_time": "2025-01-21T14:17:32.805730Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1.发起请求，例如get\n",
    "res = requests.get('http://www.baidu.com')\n",
    "res"
   ],
   "id": "5a39d696eacdcb4c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [200]>\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:10:07.117038Z",
     "start_time": "2025-01-21T14:10:07.111126Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过text属性获取实际响应体（以string形式）\n",
    "res.text"
   ],
   "id": "6a8ffad99da10cb8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<!DOCTYPE html>\\r\\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>ç\\x99¾åº¦ä¸\\x80ä¸\\x8bï¼\\x8cä½\\xa0å°±ç\\x9f¥é\\x81\\x93</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class=\"bg s_ipt_wr\"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class=\"bg s_btn_wr\"><input type=submit id=su value=ç\\x99¾åº¦ä¸\\x80ä¸\\x8b class=\"bg s_btn\"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ\\x96°é\\x97»</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>å\\x9c°å\\x9b¾</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>è§\\x86é¢\\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>è´´å\\x90§</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>ç\\x99»å½\\x95</a> </noscript> <script>document.write(\\'<a href=\"http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\\'+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ \\'\" name=\"tj_login\" class=\"lb\">ç\\x99»å½\\x95</a>\\');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style=\"display: block;\">æ\\x9b´å¤\\x9aäº§å\\x93\\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>å\\x85³äº\\x8eç\\x99¾åº¦</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>ä½¿ç\\x94¨ç\\x99¾åº¦å\\x89\\x8då¿\\x85è¯»</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>æ\\x84\\x8fè§\\x81å\\x8f\\x8dé¦\\x88</a>&nbsp;äº¬ICPè¯\\x81030173å\\x8f·&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\\r\\n'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:48:00.578100Z",
     "start_time": "2025-01-21T14:48:00.571367Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过encoding属性设置编码为utf-8\n",
    "res.encoding = 'utf-8'\n",
    "res.text"
   ],
   "id": "6ba0a79ce2ece868",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:10:12.135921Z",
     "start_time": "2025-01-21T14:10:12.130681Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过content属性获取字节码形式\n",
    "res.content"
   ],
   "id": "ce4b827547b0a9ac",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'<!DOCTYPE html>\\r\\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>\\xe7\\x99\\xbe\\xe5\\xba\\xa6\\xe4\\xb8\\x80\\xe4\\xb8\\x8b\\xef\\xbc\\x8c\\xe4\\xbd\\xa0\\xe5\\xb0\\xb1\\xe7\\x9f\\xa5\\xe9\\x81\\x93</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class=\"bg s_ipt_wr\"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class=\"bg s_btn_wr\"><input type=submit id=su value=\\xe7\\x99\\xbe\\xe5\\xba\\xa6\\xe4\\xb8\\x80\\xe4\\xb8\\x8b class=\"bg s_btn\"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>\\xe6\\x96\\xb0\\xe9\\x97\\xbb</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>\\xe5\\x9c\\xb0\\xe5\\x9b\\xbe</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>\\xe8\\xa7\\x86\\xe9\\xa2\\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>\\xe8\\xb4\\xb4\\xe5\\x90\\xa7</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>\\xe7\\x99\\xbb\\xe5\\xbd\\x95</a> </noscript> <script>document.write(\\'<a href=\"http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\\'+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ \\'\" name=\"tj_login\" class=\"lb\">\\xe7\\x99\\xbb\\xe5\\xbd\\x95</a>\\');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style=\"display: block;\">\\xe6\\x9b\\xb4\\xe5\\xa4\\x9a\\xe4\\xba\\xa7\\xe5\\x93\\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>\\xe5\\x85\\xb3\\xe4\\xba\\x8e\\xe7\\x99\\xbe\\xe5\\xba\\xa6</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>\\xe4\\xbd\\xbf\\xe7\\x94\\xa8\\xe7\\x99\\xbe\\xe5\\xba\\xa6\\xe5\\x89\\x8d\\xe5\\xbf\\x85\\xe8\\xaf\\xbb</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>\\xe6\\x84\\x8f\\xe8\\xa7\\x81\\xe5\\x8f\\x8d\\xe9\\xa6\\x88</a>&nbsp;\\xe4\\xba\\xacICP\\xe8\\xaf\\x81030173\\xe5\\x8f\\xb7&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\\r\\n'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:10:12.887073Z",
     "start_time": "2025-01-21T14:10:12.865705Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 可以调用字节码的decode方法，将其解码（默认按照utf-8格式）\n",
    "res.content.decode('utf-8')"
   ],
   "id": "5b3b7024cce26fe5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<!DOCTYPE html>\\r\\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下，你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class=\"bg s_ipt_wr\"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class=\"bg s_btn_wr\"><input type=submit id=su value=百度一下 class=\"bg s_btn\"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write(\\'<a href=\"http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\\'+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ \\'\" name=\"tj_login\" class=\"lb\">登录</a>\\');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style=\"display: block;\">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\\r\\n'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:10:13.685898Z",
     "start_time": "2025-01-21T14:10:13.680644Z"
    }
   },
   "cell_type": "code",
   "source": "type(res)",
   "id": "95de45b96e5e7388",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "requests.models.Response"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:10:37.811129Z",
     "start_time": "2025-01-21T14:10:37.807369Z"
    }
   },
   "cell_type": "code",
   "source": "res.headers",
   "id": "a1fdd0d17ea53675",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Tue, 21 Jan 2025 14:10:07 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:27:56 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:11:04.035343Z",
     "start_time": "2025-01-21T14:11:04.029757Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过status_code属性获取本次响应的状态码\n",
    "res.status_code"
   ],
   "id": "ab6b541f0f2ea361",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:12:46.327478Z",
     "start_time": "2025-01-21T14:12:46.320620Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过如下方式将其保存到一个文件中\n",
    "with open('out/baidu.html', 'w') as f:\n",
    "    f.write(res.text)"
   ],
   "id": "227ac2f2ae29c451",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:53:45.342591Z",
     "start_time": "2025-01-21T14:53:45.339304Z"
    }
   },
   "cell_type": "code",
   "outputs": [],
   "execution_count": 6,
   "source": [
    "# get请求携带参数\n",
    "payload = {'key1': 'value1', 'key2': ['value2', 'value3']}"
   ],
   "id": "27be5d5ef6843eb4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:53:50.994050Z",
     "start_time": "2025-01-21T14:53:48.282047Z"
    }
   },
   "cell_type": "code",
   "source": "res = requests.get('http://httpbin.org/get', params=payload)",
   "id": "58e4f0847a7d74d8",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:54:49.486680Z",
     "start_time": "2025-01-21T14:54:49.480490Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过其内置的json解码器解码为dict\n",
    "res_json = res.json()\n",
    "res_json"
   ],
   "id": "406a041b93faccf1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {'key1': 'value1', 'key2': ['value2', 'value3']},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate, br',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.32.3',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-678fb4fd-50bcb7937d9cc3355f950c4d'},\n",
       " 'origin': '91.103.121.35',\n",
       " 'url': 'http://httpbin.org/get?key1=value1&key2=value2&key2=value3'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-21T14:54:41.054888Z",
     "start_time": "2025-01-21T14:54:41.050005Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 可以通过使用python中dict的方式进行访问\n",
    "res_json['args']['key1']"
   ],
   "id": "503df589f7659c69",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "# 2. POST请求简单演示（表单形式）",
   "id": "42c96b7329036883"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T09:00:55.694148Z",
     "start_time": "2025-01-22T09:00:54.695892Z"
    }
   },
   "cell_type": "code",
   "source": [
    "payload = {'username': 'zhangsan', 'password': '123456', 'email': 'zhangsan@example.com'}\n",
    "res = requests.post('http://httpbin.org/post', data=payload)"
   ],
   "id": "51bdb54cecaa32c4",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T09:01:07.616934Z",
     "start_time": "2025-01-22T09:01:07.610704Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看响应结果\n",
    "res.json()"
   ],
   "id": "5fec8f0bee03477",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'data': '',\n",
       " 'files': {},\n",
       " 'form': {'email': 'zhangsan@example.com',\n",
       "  'password': '123456',\n",
       "  'username': 'zhangsan'},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate, br',\n",
       "  'Content-Length': '62',\n",
       "  'Content-Type': 'application/x-www-form-urlencoded',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.32.3',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-6790b3c8-222441d235c700fd2df882a1'},\n",
       " 'json': None,\n",
       " 'origin': '91.103.121.32',\n",
       " 'url': 'http://httpbin.org/post'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "f984b7e5432f7e44"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
